package hxg.tcptest.dome3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author HeXiangGuang
 * @create 2021-06-30 11:53
 */
public class SocketThread extends Thread{

    private Socket socket;
    public boolean flag = true;
    public SocketThread(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        if (socket != null){
            BufferedReader br =null;
            try {
                InputStream is = socket.getInputStream();
                br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                while (true){
                    String data = br.readLine();
                    //客户端已经关闭
                    if (data == null){
                        break;
                    }
                    System.out.println(socket.getInetAddress() +"说："+data);
                    if ("886".equals(data) || "byebye".equals(data)){
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    br.close();
                    socket.close();
                    System.out.println(socket.getInetAddress() +"退出了--------");
                    flag = false;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
